﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCDto.MakeStaticPagingComponent.WebsiteTemplate;
using BCService.MakeStaticPagingComponent.WebsiteTemplateService;

namespace BCTMSRESTful.Controllers.Management.MakeStaticPagingComponent
{
    /// <summary>
    /// 頁面模板控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "MakeStaticPage")]
    [Route("api/v1/sim/[controller]")]
    public class WebsiteTemplateController : ManagementBaseController
    {
        private readonly IWebsiteTemplateService websiteTemplateService;

        public WebsiteTemplateController(IWebsiteTemplateService websiteTemplateService)
        {
            this.websiteTemplateService = websiteTemplateService;
        }

        /// <summary>
        /// 获取网站模板信息
        /// </summary>
        /// <param name="id">网站模板信息Id</param>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ProducesResponseType(typeof(WebsiteTemplateDto), 200)]
        public IActionResult Get([FromRoute(Name = "id")]int id)
        {
            return this.Ok(this.websiteTemplateService.Get(id));
        }
        /// <summary>
        /// 获取使用中网站模板信息
        /// </summary>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(WebsiteTemplateDto), 200)]
        public IActionResult InUse()
        {
            return this.Ok(this.websiteTemplateService.GetUsed());
        }
        /// <summary>
        /// 获取网站模板信息列表
        /// </summary>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<WebsiteTemplateDto>), 200)]
        public IActionResult Get()
        {
            return this.Ok(this.websiteTemplateService.Get());
        }
        
        /// <summary>
        /// 获取网站模板信息列表
        /// </summary>
        /// <param name="templateName">模板名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="minPrice">最低价格</param>
        /// <param name="maxPrice">最高价格</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <response code="200">返回网站配置服务信息。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<WebsiteTemplateDto>), 200)]
        public IActionResult Query([FromQuery(Name = "template_name")]string templateName,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "min_price")]decimal? minPrice,
            [FromQuery(Name = "max_price")]decimal? maxPrice,
            [FromQuery(Name = "sort_field")]string sortField,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index=1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.websiteTemplateService.Get(templateName, minPrice,maxPrice,start,end,sortField,ascending,index,size));
        }
    
        /// <summary>
        /// 添加网站模板信息
        /// </summary>
        /// <param name="websiteTemplateRequestDto">网站模板信息</param>
        /// <response code="201">返回网站模板信息。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(WebsiteTemplateDto), 201)]
        public IActionResult Post([FromBody]WebsiteTemplateRequestDto websiteTemplateRequestDto)
        {
            WebsiteTemplateDto result = this.websiteTemplateService.Add(websiteTemplateRequestDto);
            return this.Created(string.Format("api/v1/sim/WebsiteTemplate/{0}", result.WebsiteTemplateId), result);
        }

        /// <summary>
        /// 修改网站模板信息
        /// </summary>
        /// <param name="id">网站模板信息Id</param>
        /// <param name="websiteTemplateRequestDto">网站模板信息</param>
        /// <response code="200">返回网站模板信息。</response>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(typeof(WebsiteTemplateDto), 200)]
        public IActionResult Put([FromRoute(Name = "id")]int id, [FromBody]WebsiteTemplateRequestDto websiteTemplateRequestDto)
        {
            return this.Ok(this.websiteTemplateService.Update(id, websiteTemplateRequestDto));
        }
       
        /// <summary>
        /// 应用网站模板
        /// </summary>
        /// <param name="id">网站模板信息Id</param>
        /// <response code="204">应用网站模板成功。</response>
        /// <returns></returns>
        [HttpPut("[action]/{id}")]
        public IActionResult ApplyWebsiteTemplet([FromRoute(Name = "id")]int id)
        {
            this.websiteTemplateService.UpdateWebsiteTemplateIsUsed(id);
            return this.NoContent();
        }
        
        /// <summary>
        /// 删除网站模板信息
        /// </summary>
        /// <param name="id">网站模板信息Id</param>
        /// <response code="204">没有返回信息。</response>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult Delete([FromRoute(Name ="id")]int id)
        {
            this.websiteTemplateService.Delete(id);
            return this.NoContent();
        }
    }
}
